Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports ASPNET.StarterKit.BusinessLogicLayer

Partial Public Class User_Create_aspx
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        ' disable the UI for anonymous user when the config switch is disabled

        If (Not Page.User.Identity.IsAuthenticated) AndAlso String.Compare(ConfigurationManager.AppSettings("AllowUserCreationForAnonymousUsers"), "0") = 0 Then
            noAccessMsg.Visible = True
            CreateUserWizard1.Visible = False
        End If

        If (Not Page.IsPostBack) Then
            If Page.User.IsInRole("ProjectAdministrator") Then
                CreateUserWizard1.LoginCreatedUser = False
                GroupName.SelectedValue = GetDefaultRoleForNewUser()
            End If
        End If

    End Sub

    Protected Sub AddUserToRole(ByVal newUserName As String, ByVal roleInformation As String)

        Select Case roleInformation

            Case "0"
                Roles.AddUserToRole(newUserName, "ProjectAdministrator")
                Roles.AddUserToRole(newUserName, "ProjectManager")
                Roles.AddUserToRole(newUserName, "Consultant")

            Case "1"
                Roles.AddUserToRole(newUserName, "ProjectManager")
                Roles.AddUserToRole(CreateUserWizard1.UserName, "Consultant")

            Case Else
                Roles.AddUserToRole(CreateUserWizard1.UserName, "Consultant")

        End Select

    End Sub

    Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles CreateUserWizard1.CreatedUser

        If (Not Page.User.IsInRole("ProjectAdministrator")) Then
            AddUserToRole(CreateUserWizard1.UserName, GetDefaultRoleForNewUser())
            'CreateUserWizard1.ActiveStepIndex = CreateUserWizard1.WizardSteps.IndexOf(CreateUserWizard1.CompleteStep)
        End If

    End Sub

    Protected Sub CreateUserWizard1_NextStepHandler(ByVal sender As Object, ByVal e As EventArgs) Handles CreateUserWizard1.NextButtonClick

        If (CreateUserWizard1.ActiveStepIndex = 1) And (Not Page.User.IsInRole("ProjectAdministrator")) Then
            CreateUserWizard1.ActiveStepIndex = CreateUserWizard1.WizardSteps.IndexOf(CreateUserWizard1.CompleteStep)
        End If
    End Sub

    Protected Sub Wizard_FinishButton_Click(ByVal sender As Object, ByVal e As WizardNavigationEventArgs) Handles CreateUserWizard1.FinishButtonClick

        ' Create an empty Profile for the newly created user
        Dim p As ProfileCommon = DirectCast(ProfileCommon.Create(CreateUserWizard1.UserName, True), ProfileCommon)

        p.FirstName = Server.HtmlEncode(FirstName.Text)
        p.LastName = Server.HtmlEncode(LastName.Text)
        p.EmpId = Server.HtmlEncode(EmpId.Text)
        p.Save()

        If Page.User.IsInRole("ProjectAdministrator") Then
            AddUserToRole(CreateUserWizard1.UserName, GroupName.SelectedValue)
        End If

    End Sub

    Private Function GetDefaultRoleForNewUser() As String

        If ConfigurationManager.AppSettings("DefaultRoleForNewUser") Is Nothing Then
            Throw New Exception("DefaultRoleForNewUser was not been defined in the appsettings section of config")
        Else
            Dim defaultRole As String = ConfigurationManager.AppSettings("DefaultRoleForNewUser")
            If String.IsNullOrEmpty(defaultRole) Then
                Throw New Exception("DefaultRoleForNewUser does not contain a default value")
            Else
                If String.Compare(defaultRole, "3") < 0 AndAlso String.Compare(defaultRole, "0") >= 0 Then
                    Return (ConfigurationManager.AppSettings("DefaultRoleForNewUser"))
                Else
                    Throw New ArgumentException("DefaultRoleForNewUser defined in the appsettings has to be between 0 and 2")
                End If
            End If
        End If

    End Function

End Class
